#include "unixhead.h"

#include <workflow/WFFacilities.h>
#include <workflow/WFTaskFactory.h>

#include <iostream>
using namespace std;
//using namespace ;

static WFFacilities::WaitGroup waitGroup(1);

void sighandler(int signum)
{
    cout << "wait group done\n";
    waitGroup.done();//每执行一次done，任务会减少1个，减为0时，wait就会返回
}

void callback(WFHttpTask * httptask)
{
    printf("callback is running\n");
    //获取请求和响应
    auto req = httptask->get_req();
    auto response = httptask->get_resp();
    //http任务执行的过程中，获取其状态
    int state = httptask->get_state();
    //获取其错误码
    int error = httptask->get_error();

    switch(state){
    case WFT_STATE_SYS_ERROR:
        fprintf(stderr, "system error: %s\n", strerror(error));break;
    case WFT_STATE_DNS_ERROR:
        fprintf(stderr, "dns error: %s\n", gai_strerror(error));break;
    case WFT_STATE_SUCCESS:
        break;
    }

    if(state != WFT_STATE_SUCCESS) {
        fprintf(stderr, "error occurs!\n");
        return;
    }


}

int main()
{
    signal(SIGINT, sighandler);

    //创建了一个HTTP任务对象
    auto httpTask = WFTaskFactory::create_http_task("http://www.baidu.com", 0, 0, callback);
    //设置任务的属性
    auto req = httpTask->get_req();
    req->set_method("POST");
    req->add_header_pair("key1", "val1");
    //将任务交给框架去运行
    httpTask->start();

    waitGroup.wait();//阻塞当前进程

    return 0;
}